<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button
              slot="append"
              @click="getgoodsList"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addPage"
            >添加商品</el-button
          ></el-col
        >
      </el-row>
      <!--  商品列表表格 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>

        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" width="95px" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" width="70px" label="商品重量">
        </el-table-column>
        <el-table-column width="140px" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormate }}
          </template>
        </el-table-column>

        <el-table-column width="130px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="removeById(scope.row.goods_id)"
              icon="el-icon-delete"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tatol"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'VueShopGoods',



  data() {
    return {

      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //商品列表数据
      goodsList: [],
      // 总条数
      tatol: 0,



    }
  },
  created() {
    this.getgoodsList()
  },

  mounted() {


  },

  methods: {

    // 获取商品列表数据
    async getgoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) return this.$message({ showClose: true, message: res.meta.message, type: 'error' })

      this.goodsList = res.data.goods
      this.tatol = res.data.total



    },
    // 改变分页的条数
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getgoodsList()
    },
    //  改变分页的页数
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page
      this.getgoodsList()

    },
    // 删除商品
    removeById(id) {

      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) {
          return this.$message({ showClose: true, message: '删除失败', type: 'error' })
        }
        this.getgoodsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })


    },
    addPage() {
      this.$router.push('/goods/add')
    }

  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 15px 0;
}
</style>